package com.siae.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.siae.mapper.AutditMapper;
import com.siae.pojo.Audit;
import com.siae.pojo.vo.Result;
import com.siae.service.AuditService;
import com.siae.service.ItemService;
import com.siae.utils.BizCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class AuditServiceImpl extends ServiceImpl<AutditMapper, Audit> implements AuditService {
    @Autowired
    private AutditMapper auditMapper;
    @Autowired
    private ItemService itemService;

    @Override
    public Result<List<Audit>> getAllAudits(Integer status) {
        try {
            // 创建条件对象，设置查询条件参数
            QueryWrapper<Audit> wrapper = new QueryWrapper<>();
            wrapper.like(status != null,"status",status);
            List<Audit> auditList = auditMapper.selectList(wrapper);
            return Result.success(BizCode.SUCCESS,auditList);
        } catch (Exception e) {
            log.error("查询审核信息失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result<Audit> getAuditById(Integer id) {
        try {
            Audit audit = auditMapper.selectById(id);
            return Result.success(BizCode.SUCCESS,audit);
        } catch (Exception e) {
            log.error("查询审核信息失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result addAudit(Audit audit) {
        try {
            audit.setAuditSize(0);
            audit.setCreatedAt(new Date());
            audit.setUpdatedAt(new Date());
            audit.setIsDel(0);
            auditMapper.insert(audit);
            return Result.success(BizCode.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("添加审核信息失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result updateAudit(Audit audit) {
        try {
            audit.setUpdatedAt(new Date());
            auditMapper.updateById(audit);
            return Result.success(BizCode.SUCCESS);
        } catch (Exception e) {
            log.error("修改审核信息失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }

    @Override
    public Result deleteListAudit(List idList) {
        try {
            auditMapper.deleteByIds(idList);
            return Result.success(BizCode.SUCCESS);
        } catch (Exception e) {
            log.error("删除审核信息失败：{}",e.getMessage());
            return Result.fail(BizCode.SYSTEM_ERROR);
        }
    }
}
